package com.engine.salary.formlua.func.compare;

import com.engine.salary.formlua.entity.parameter.DataType;
import com.engine.salary.formlua.util.CompareUtil;
import com.engine.salary.formlua.util.DateUtil;
import com.engine.salary.formlua.util.ExcelParamUtil;
import com.ql.util.express.instruction.op.OperatorEqualsLessMore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.Date;
import java.util.List;


public class GreaterEqOperator extends OperatorEqualsLessMore {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public Object executeInner(Object op1, Object op2) throws Exception {
        DataType result = new DataType();
        result.setDataType(DataType.BOOL);
        String datatype = ExcelParamUtil.checkParamType(op1);
        //对option的特殊处理，获取option的字符串后根据逗号分割，然后排序数组，数组长度不一致返回false，一致则循环对比直到同样下标的数据不一致返回false，否则为true
        try {
            if (datatype.equals(DataType.OPTION)) {
                result.setContent(true);
                String[] firstOptions = new String[]{};
                String[] secondOptions = new String[]{};
                firstOptions = CompareUtil.genArray(op1, "option");
                secondOptions = CompareUtil.genArray(op2, "option");
                Arrays.sort(firstOptions);
                Arrays.sort(secondOptions);
                if (firstOptions.length != secondOptions.length) {
                    result.setContent(false);
                } else {
                    int i = 0;
                    for (; i < firstOptions.length; i++) {
                        boolean loopresult = OperatorEqualsLessMore.executeInner(">=", firstOptions[i], secondOptions[i]);
                        if (!loopresult) {
                            result.setContent(false);
                            break;
                        }
                    }
                }
            } else if (op1 instanceof DataType && op2 instanceof DataType && DateUtil.isDateComponent(op1) && DateUtil.isDateComponent(op2)) {


                Object dateStringOne = null;
                if (op1 instanceof DataType) {
                    DataType dataTypeOne = (DataType) op1;
                    dateStringOne = dataTypeOne.getContent();
                } else {
                    dateStringOne = op1;
                }

                Object dateStringTwo = null;
                if (op2 instanceof DataType) {
                    DataType dataTypeTwo = (DataType) op2;
                    dateStringTwo = dataTypeTwo.getContent();
                } else {
                    dateStringTwo = op2;
                }
                boolean dateResult = false;
                Date firstDate = DateUtil.buildDateByDateChar(dateStringOne == null ? "" : dateStringOne + "");
                Date secondDate = DateUtil.buildDateByDateChar(dateStringTwo == null ? "" : dateStringTwo + "");
                if (firstDate == null && secondDate == null) {
                    dateResult = true;
                } else if (firstDate == null && secondDate != null) {
                    dateResult = false;
                } else if (firstDate != null && secondDate == null) {
                    dateResult = true;
                } else {
                    dateResult = firstDate.getTime() >= secondDate.getTime();
                }
                result.setContent(dateResult);
            } else {
                List<Object> dataList = ExcelParamUtil.converParamValue(op1, op2);
                boolean r;
                if (ExcelParamUtil.getParamType(op1).toLowerCase().equals("double") || ExcelParamUtil.getParamType(op2).toLowerCase().equals("double")) {
                    r = OperatorEqualsLessMore.executeInner(">=", ExcelParamUtil.convertParamValToNumber(dataList.get(0)), ExcelParamUtil.convertParamValToNumber(dataList.get(1)));
                } else {
                    r = OperatorEqualsLessMore.executeInner(">=", ExcelParamUtil.getParamContent(dataList.get(0), ""), ExcelParamUtil.getParamContent(dataList.get(1), ""));
                }
                result.setContent(r);
            }
        } catch (Exception e) {
            logger.error("err", e);
            result.setContent(false);
        }
//		Compareutils.buildLessMoreFilterParam(result,this.name,op1,op2);
        return result.getContent();
    }

    public GreaterEqOperator(String aAliasName, String aName, String aErrorInfo) {
        super(aAliasName, aName, aErrorInfo);
    }

    public GreaterEqOperator(String aName) {
        super(aName);
    }


}
